package com.yusong.algorithm;


/**
 * 排序抽象类
 */
public abstract class AbstractSort {

    /**
     * 排序方式
     *
     * @param a
     */
    public abstract void sort(Comparable[] a);


    /**
     * 小于
     *
     * @return
     */
    protected static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    /**
     * 打印
     *
     * @param a
     */
    protected static void show(Comparable[] a) {
        int length = a.length;
        for (int i = 0; i < length; i++) {
            System.out.println(a[i] + " ");
        }
        System.out.println("\n");
        System.out.println(isSort(a));
    }

    /**
     * 判断是否排序成功
     */
    protected static boolean isSort(Comparable[] a) {
        int length = a.length;
        for (int i = 1; i < length; i++) {
            if (less(a[i], a[i - 1])) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    /**
     * 交换两个元素的位置
     */
    protected static void exchange(Comparable[] a, int i, int j) {
        Comparable tempt = a[j];
        a[j] = a[i];
        a[i] = tempt;
    }

    /**
     * 复制数组
     */
    protected static Comparable[] copy(Comparable[] source, Comparable[] target) {
        int length = source.length;
        if(target == null){
            target = new Comparable[length];
        }
        if (length != target.length) {
            System.out.println("数组大小不一样，不能复制");
            return target;
        }
        for (int i = 0; i < length; i++) {
            target[i] = source[i];
        }
        return target;
    }


}
